﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ComeSeeThis_MVC2.Models;

namespace ComeSeeThis_MVC2.Controllers
{
    [Authorize(Roles = "SiteAdmin")]
    public class SecurityController : Controller
    {
        // ReSharper disable InconsistentNaming
        readonly SecurityRepository securityRepository = new SecurityRepository();
        readonly SecurityRepository securityRepository2 = new SecurityRepository();
        // ReSharper restore InconsistentNaming

        public ActionResult Index()
        {
            return View();
        }

        #region Operations

        public ActionResult Operations()
        {
            var operations = securityRepository.GetAllOperations();

            return View(operations);
        }

        public ActionResult OperationDetails(Guid id)
        {
            var operation = securityRepository.GetOperation(id);
            if (operation == null) return View("OperationNotFound");

            return View(operation);

        }

        public ActionResult OperationAdd()
        {
            return View();
        }
        [HttpPost]
        public ActionResult OperationAdd(aspnet_Operation operation, FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                operation.OperationId = Guid.NewGuid();
                securityRepository.OperationAdd(operation);
                securityRepository.Save();

                // All operations are automatically added to the role of SiteAdmin
                //var newOperation = securityRepository.GetOperation(operation.Code);
                var siteAdmin = securityRepository.GetRole("SiteAdmin");
                siteAdmin.aspnet_Operations.Add(operation);

                securityRepository.Save();
                return RedirectToAction(formCollection["addAnother"] == "on" ? "OperationAdd" : "Operations");
            }
            return View("OperationAdd");
        }

        public ActionResult OperationEdit(Guid id)
        {
            var operation = securityRepository.GetOperation(id);
            if (operation == null) return View("OperationNotFound");

            operation.Code = operation.Code.Trim();
            operation.Name = operation.Name.Trim();

            return View(operation);
        }
        [HttpPost]
        public ActionResult OperationEdit(aspnet_Operation operation)
        {
            var newOp = securityRepository.GetOperation(operation.OperationId);

            if (TryUpdateModel(newOp))
            {
                newOp.Code = newOp.Code.ToUpper();
                securityRepository.Save();

                return RedirectToAction("Operations");
            }
            return View();
        }

        public ActionResult OperationDelete(Guid id)
        {
            var operation = securityRepository.GetOperation(id);

            return operation == null ? View("OperationNotFound") : View(operation);
        }
        [HttpPost]
        public ActionResult OperationDelete(Guid id, string confirmButton)
        {
            var operation = securityRepository.GetOperation(id);
            if (operation == null) return View("OperationNotFound");

            securityRepository.OperationDelete(operation);
            securityRepository.Save();

            return RedirectToAction("Operations");
        }

        public ActionResult SubmitOperation()
        {
            return View();
        }
        public ActionResult DeleteOperation()
        {
            return View();
        }

        #endregion

        #region Roles

        public ActionResult Roles()
        {
            var roles = securityRepository.GetAllRoles();

            return View(roles);
        }

        public ActionResult RoleAdd()
        {
            return View();
        }
        [HttpPost]
        public ActionResult RoleAdd(aspnet_Roles role, FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                role.RoleId = Guid.NewGuid();
                role.ApplicationId = new Guid("b824d83c-d9d1-4521-9d0c-0ee9324a581d");
                role.LoweredRoleName = role.RoleName.ToLower();
                securityRepository.RoleAdd(role);
                securityRepository.Save();

                return RedirectToAction(formCollection["addAnother"] == "on" ? "RoleAdd" : "Roles");
            }
            return View("RoleAdd");
        }

        public ActionResult RoleEditOld(Guid id)
        {
            var role = securityRepository.GetRole(id);
            if (role == null) return View("RoleNotFound");

            role.RoleName = role.RoleName.Trim();

            return View(role);
        }
        [HttpPost]
        public ActionResult RoleEditOld(aspnet_Roles role)
        {
            var newOp = securityRepository.GetRole(role.RoleId);

            if (TryUpdateModel(newOp))
            {
                securityRepository.Save();

                return RedirectToAction("Roles");
            }
            return View();
        }

        public ActionResult RoleEdit(Guid id)
        {
            var role = securityRepository.GetRole(id);
            if (role == null) return View("RoleNotFound");

            role.RoleName = role.RoleName.Trim();

            return View(new RoleFormView(role));
        }
        [HttpPost]
        public ActionResult RoleEdit(aspnet_Roles role)
        {
            var newRole = securityRepository.GetRole(role.RoleId);

            if (TryUpdateModel(newRole))
            {
                securityRepository.Save();

                return RedirectToAction("Roles");
            }
            return View();
        }

        [HttpPost]
        public ActionResult SubmitOperation(Guid roleId, Guid operationId)
        {
            if (Request.IsAjaxRequest())
            {
                var context = new AspNetStuffEntities();
                var role = context.aspnet_Roles.Where(x => x.RoleId == roleId).First();
                var operation = context.aspnet_Operation.Where(x => x.OperationId == operationId).First();

                role.aspnet_Operations.Add(operation);

                context.SaveChanges();

                return View("OperationList", new OperationForm(roleId));
            }
            return View("RoleEdit", roleId);

        }

        [HttpPost]
        public ActionResult DeleteOperation(Guid id, Guid operationId)
        {
            if (Request.IsAjaxRequest())
            {
                var context = new AspNetStuffEntities();
                var role = context.aspnet_Roles.Where(x => x.RoleId== id).First();
                var operation = context.aspnet_Operation.Where(x => x.OperationId == operationId).First();

                role.aspnet_Operations.Remove(operation);

                context.SaveChanges();

                return View("OperationList", new OperationForm(id));
            }
            return View("RoleEdit", id);
        }

        public ActionResult RoleDelete(Guid id)
        {
            var role = securityRepository.GetRole(id);

            return role == null ? View("RoleNotFound") : View(role);
        }
        [HttpPost]
        public ActionResult RoleDelete(Guid id, string confirmButton)
        {
            var role = securityRepository.GetRole(id);
            if (role == null) return View("RoleNotFound");

            securityRepository.RoleDelete(role);
            securityRepository.Save();

            return RedirectToAction("Roles");
        }

        #endregion
    }
}
